﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Net.Mail;

namespace EmailSendWizard
{
   public class EmailSentAction
    {
        SmtpClient mySmtpClient;
        public EmailSentAction(string smtpServerName, String userName, String pw, int port, Boolean sslEA)
        {
            //Constructor: Manage the SMTP server
            //Set SMTP
            mySmtpClient = new SmtpClient(smtpServerName, port);
            mySmtpClient.Credentials = new NetworkCredential(userName, pw);
            mySmtpClient.EnableSsl = sslEA;
        }

        public void SendMail(String senderEmail, String receiverEmail, String subject, String body)
        {

            if (receiverEmail != null && receiverEmail.Contains("@"))
            {
                MailAddress to = new MailAddress(receiverEmail);
                MailAddress from = new MailAddress(senderEmail);
                MailMessage msg = new MailMessage();
                msg.Priority = MailPriority.High;
                msg.Subject = subject;
                msg.Body = body;
                msg.IsBodyHtml = false;
                msg.From = from;
                msg.To.Add(to);
                try
                {
                    mySmtpClient.Send(msg);
                    System.Windows.Forms.MessageBox.Show("Send success!");
                }
                catch (System.Net.Mail.SmtpException ex)
                {
                    System.Windows.Forms.MessageBox.Show("Not success, Please Check everything again! Exception: " + ex);
                    throw;
                }
            }
            else
            {
                System.Windows.Forms.MessageBox.Show("Invalid email address");
            }
        }
    }
}
